var util =
{
	letters: "abcdefghijklmnopqrstuvwxyz",
	wordWrap: function(str, width)
	{
		var regex = '.{1,' + width + '}(\\s|$)|.{' + width + '}|.+$';
		return str.match(RegExp(regex, 'g'));
	},
	bytesToKb: function(bytes)
	{
		return Math.floor((bytes / 1024) * 10) / 10;
	}
};

// Extension methods
if(!Array.indexOf)
{
	Array.prototype.indexOf = function(obj)
	{
		for(var i = 0; i < this.length; ++i)
		{
			if(this[i] == obj)
				return i;
		}
		return -1;
	}
}

/** Repeatable Random Number Generator.
@param seed The initial seed value (optional).
*/
function Rrng(seed)
{
	if(seed !== undefined)
		this._last = seed;
	else
		this._last = 59051;
}
Rrng.prototype =
{
	/** Gets the next random number in the sequence.
	@param p1 Inclusive minimum (optional).
	@param p2 Exclusive maximum (optional).
	*/
	next: function(p1, p2)
	{
		this._last = (251 * this._last + 32749) % 65521;
		if(p1 != undefined)
		{
			if(p2 != undefined)
				if(p2 == p1)
					return p1;
				else
					return (this._last % (p2 - p1)) + p1;
			else
				return this._last % p1;
		}
		return this._last;
	},
	/** Gets the next float in the sequence in the range 0 to 1 inclusive.
	
	The next float is calculated by getting the next integer from 0 to 10000
	and dividing that by 10000.
	*/
	nextFloat: function()
	{
		return this.next(0, 10001) / 10000;
	},
	/** Gets a random element from an array.
	
	The array element is calculated by getting the integer from 0 to
	array.length and indexing the array by this value.
	*/
	nextElement: function(array)
	{
		return array[this.next(0, array.length)];
	},
	/** Sets the seed.
	@param seed The new seed value.
	*/
	seed: function(seed)
	{
		this._last = seed;
	}
}

/** Global Rrng object which MUST NOT BE USED for generation as it will change
 *on every load.
 */
var rand = new Rrng(Math.floor(Math.random() * 65521));
